<template>
	<gracePage :customHeader="false">
		<!-- 页面主体 -->
		<view class="grace-body" slot="gBody">
			<view>
				<graceNavBar :items="navItems" fontSize="40rpx" activeFontSize="40rpx"
				lineHeight="80rpx" :isCenter="true" 
				:currentIndex="navIndex" :size="0" padding="20rpx" 
				activeLineBg="#3688FF" textAlign="center"
				activeColor="#3688FF" activeLineWidth="100%" activeLineHeight="2rpx" 
				:margin="10" @change="navChange"></graceNavBar>
			</view>
			<graceEmptyNew v-if="itemNull">
				<view slot="img" class="empty-view">
					<image class="empty-img" mode="widthFix" src="../../static/empty.png"></image>
				</view>
				<text slot="text" class="grace-text-small grace-gray">抱歉，没有搜索到记录</text>
			</graceEmptyNew>			
			<view v-if="!itemNull" class="grace-list" v-for="(item, index) in objects" :key="index" @tap="detail(item)">
				<view class="grace-list-items">
					<view class="grace-list-image grace-relative">
						<image class="grace-list-image" :src="getImgUrl(item)"></image>
					</view>
					<view class="grace-list-body grace-border-b">
						<view class="grace-list-title">
							<text class="grace-list-title-text" :style="{color:getNotifyTitleColor(item)}">{{item.field1}}</text>
							<text class="grace-list-title-desc">{{item.field15}}</text>
						</view>
						<view class="grace-list-body-desc">{{item.createTime}}</view>
					</view>
					<text class="grace-list-arrow-right grace-icons icon-arrow-right"></text>
				</view>
			</view>		
		</view>
	</gracePage>
</template>
<script>
import gracePage from "../../graceUI/components/gracePage.vue";
import graceFlex from "../../graceUI/components/graceFlex.vue";
import graceNavBar from "../../graceUI/components/graceNavBar.vue";
var _self;
export default {
	data() {
		return {
			token:"",
			pageNum : 1,
			//每次加载的记录数
			pageSize : 10,
			//解决列表触底加载过程中服务器新增数据问题
			createTimeTo : "",
			userInfoData:"",
			showLoadMore:false,
			loadMoreText:"加载更多",
			navItems : ['全部', '待付款', '使用中', '已完成'],
			navIndex : 0,
			itemNull : false,
			phone : '',
			objects : [],
			notifyTitleColor:""
		}
	},
	onLoad:function(e){
		_self = this;
		try{
			_self.userInfoData = _self.$myFunction.appStorage('wuLuUserInfo');						
			_self.phone = _self.userInfoData.field2;
			_self.createTimeTo = _self.$myFunction.nowTime('Y-m-d H:i:s');
			_self.getList(_self.phone,'');		
		}catch(e){
			_self.itemNull = true;
		}	
	},
	onShow:function(e){
		uni.$on('orderListRefresh',function(data){
			try{
				_self.userInfoData = _self.$myFunction.appStorage('wuLuUserInfo');
				_self.$myFunction.displayLog("支付成功后刷新【订单列表】界面，从缓存中读取到的用户数据："+JSON.stringify(_self.userInfoData));
				_self.phone = _self.userInfoData.field2;
				_self.createTimeTo = _self.$myFunction.nowTime('Y-m-d H:i:s');
				_self.getList(_self.phone,'');		
			}catch(e){
				_self.itemNull = true;
			}
		})	
	},
	methods:{
		navChange : function (index){
			_self.navIndex = index;
			switch (_self.navIndex){
				case 0:
					_self.getList(_self.phone,'');
					break;
				case 1:
					_self.getList(_self.phone,"creating");
					break;
				case 2:
					_self.getList(_self.phone,"paySuccess");
					break;	
				case 3:
					_self.getList(_self.phone,"finish");
					break;															
				default:
					break;
			}
		},
		//获取图片地址
		getImgUrl: function(item){
			var imageUrl =  _self.$myFunction.appConfig().serverIp+"resource/youXiangWeiShi/productionImage/"+item.field2+"/1.jpg";
			return imageUrl;
		},
		getNotifyTitleColor: function(item){
			if(item.field15 == '待付款'){
				return "red";
			}
		},
		getList: function(temp,state){
			uni.showToast({
				title: '请稍后...',
				duration:_self.$myFunction.appConfig().requestWaitTime,
				icon: 'none',
			});
			var params = {
				'field1':'',
				'field2':'',
				'field3':'油箱卫士',
				'field4':'',
				'field5':'',
				'field6':'',
				'field7':'',
				'field8':'',
				'field9':'',
				'field10':'',
				'field11':'',
				'field12':'',
				'field13':'',
				'field14':'',
				'field15':state,
				'field16':'',
				'field17':'',
				'field18':temp,
				'field19':'',
				'field20':'',
				'status':"1",
				'createTimeFrom':'',
				'createTimeTo':_self.createTimeTo,
				'pageNum':_self.pageNum,
				'pageSize':_self.pageSize,
				'order':'desc',
				'field':"createTime"
			};
			var paramsData = {
				"jsonParas":_self.$AesUtil.encrypt(encodeURIComponent(JSON.stringify(params)))
			}
			//重新获取一次token，防止接口返回认证失败系统重新获取的token未被使用
			_self.token = _self.$myFunction.appStorage('wuLuToken').token;
			uni.request({
				url:  _self.$myFunction.appConfig().serverAddress+'productionOrderList',
				data: paramsData,
				method : "POST",
				header : {
					'content-type':'application/x-www-form-urlencoded',
					'Authentication':_self.token
				},		
				success: data => {
					// 刷新完毕后关闭刷新
					uni.hideToast();
					uni.stopPullDownRefresh();
					_self.$myFunction.displayLog("服务器返回数据："+JSON.stringify(data));					
					if (data.data.code == 200) {
						//总记录数
						var total = data.data.data.total;
						_self.$myFunction.displayLog("服务器返回数据总记录数："+total);
						var objectsData=data.data.data.rows;
						_self.$myFunction.displayLog("服务器返回当前查询条件记录数："+objectsData.length);						
						if(total == 0){//查询到0条记录
							_self.itemNull = true;
							return;
						}else{
							_self.itemNull = false;
							if(_self.showLoadMore){//触底加载数据
								// if(_self.objects.length == total){
								// 	_self.loadMoreText = "没有更多数据了";
								// 	_self.pageNum = 1;
								// }else if(_self.objects.length < total){//后台有未加载数据
								// 	//以用户打开界面那一刻作为时间参考点
								// 	//两种情况：
								// 	//1、后台有未加载历史数据
								// 	//2、后台有新增数据（此种情况不处理，因为需要刷新列表才能将最新数据展示在最前方）
								// 	//listObjectNum:总记录数-当前列表记录数
								// 	for (let i = 0; i < objectsData.length; i++) {
										
								// 		// orderList = [
								// 		// 	{
								// 		// 		time: '2019-04-06 11:37',
								// 		// 		state: 9,
								// 		// 		goodsList: [{
								// 		// 			title: '葡萄',
								// 		// 			price: 179.5,
								// 		// 			image: '/static/temp/banner1.jpg',
								// 		// 			number: 1,
								// 		// 			attr: '1斤'
								// 		// 		}]
								// 		// 	}
								// 		// ],
										
								// 		let orderListTemp = {};
										
								// 		//订单信息
								// 		orderListTemp.id = objectsData[i].id
								// 		orderListTemp.time = objectsData[i].createTime;
								// 		if(objectsData[i].field15 == 'creating'){
								// 			orderListTemp.state = "待支付";
								// 		}else if(objectsData[i].field15 == 'paySuccess'){
								// 			orderListTemp.state = "待收货";
								// 		}else if(objectsData[i].field15 == 'cancel'){
								// 			orderListTemp.state = "已取消";
								// 		}else if(objectsData[i].field15 == 'finish'){
								// 			orderListTemp.state = "已完成";
								// 		}
								// 		//产品信息
								// 		let goodsListTemp = {};
								// 		goodsListTemp = JSON.stringify(objectsData[i].field17);
										
								// 		// 团购产品信息=购物车信息
								// 		// cartList = [{
								// 		// 		id: 1,
								// 		// 		image: '/static/temp/banner1.jpg',
								// 		// 		attr_val: '水果',
								// 		// 		stock: 15,
								// 		// 		title: '水果',
								// 		// 		price: 278.00,
								// 		// 		number: 1
								// 		// 	}],
								// 		let goodsList = [];
								// 		let goodsListTemp1 = JSON.parse(goodsListTemp);
								// 		let goodsListTemp2 = JSON.parse(goodsListTemp1);
								// 		goodsList.push(goodsListTemp2);
										
								// 		orderListTemp.goodsList = goodsList;
								// 		//全部订单
								// 		_self.navList[0].orderList.push(orderListTemp);
										
								// 		if(objectsData[i].field15 == 'creating'){
								// 			//待支付订单
								// 			_self.navList[1].orderList.push(orderListTemp);
								// 		}else if(objectsData[i].field15 == 'paySuccess'){
								// 			//待收货订单
								// 			_self.navList[2].orderList.push(orderListTemp);
								// 		}
										
								// 	}
								// 	//返回的行数小于pageSize，证明数据已全部加载完毕
								// 	if(objectsData.length<_self.pageSize){
								// 		_self.loadMoreText = "没有更多数据了";
								// 	}else{
								// 		_self.loadMoreText = "加载更多";
								// 	}
								// }else if(_self.objects.length > total){//后台有删除数据
								// 	//后台有数据被删除时不做数据处理，即前端显示有已被删除的数据
								// 	//此时，当点击记录查看详情时应提示"此发布已删除"
								// 	_self.loadMoreText = "没有更多数据了";
								// }
							}else{//普通重新加载
								// _self.navList[0].orderList.splice(0);
								// _self.navList[1].orderList.splice(0);
								// _self.navList[2].orderList.splice(0);
								_self.objects.splice(0);	
								for (let i = 0; i < objectsData.length; i++) {
									
									// orderList = [
									// 	{
									// 		time: '2019-04-06 11:37',
									// 		state: 9,
									// 		goodsList: [{
									// 			title: '葡萄',
									// 			price: 179.5,
									// 			image: '/static/temp/banner1.jpg',
									// 			number: 1,
									// 			attr: '1斤'
									// 		}]
									// 	}
									// ],
									
									// let orderListTemp = {};
									
									//订单信息
									// orderListTemp.id = objectsData[i].id
									// orderListTemp.time = objectsData[i].createTime;
									// if(objectsData[i].field15 == 'creating'){
									// 	orderListTemp.state = "待支付";
									// }else if(objectsData[i].field15 == 'paySuccess'){
									// 	orderListTemp.state = "待收货";
									// }else if(objectsData[i].field15 == 'cancel'){
									// 	orderListTemp.state = "已取消";
									// }else if(objectsData[i].field15 == 'finish'){
									// 	orderListTemp.state = "已完成";
									// }

									// //全部订单
									// _self.navList[0].orderList.push(orderListTemp);
									
									// if(objectsData[i].field15 == 'creating'){
									// 	//待支付订单
									// 	_self.navList[1].orderList.push(orderListTemp);
									// }else if(objectsData[i].field15 == 'paySuccess'){
									// 	//待收货订单
									// 	_self.navList[2].orderList.push(orderListTemp);
									// }
									
									if(objectsData[i].field15 == 'creating'){
										objectsData[i].field15 = "待付款";
									}else if(objectsData[i].field15 == 'paySuccess'){
										objectsData[i].field15 = "使用中";
									}else if(objectsData[i].field15 == 'cancel'){
										objectsData[i].field15 = "已取消";
									}else if(objectsData[i].field15 == 'finish'){
										objectsData[i].field15 = "已完成";
									}
									_self.objects.push(objectsData[i]);
									
								}
								_self.$myFunction.displayLog("处理后的订单信息："+JSON.stringify(_self.objects));					
							}
						}
					}else{
						//如果请求数据因token认证失败，则清空缓存token
						if(data.data.message.indexOf("接口访问未认证") != -1){
							uni.setStorage({
								key: 'wuLuToken',
								data: "",
								success: function () {	
									_self.$myFunction.displayLog("清空缓存token");
									//触发重新获取token
									_self.$myFunction.getToken();
									// 关闭所有页面，打开到应用内的某个页面。
									_self.$myFunction.displayLog("重加载当前页面");
									uni.reLaunch({
										url: '/pages/order/order'
									});
								}
							});
						}else{
							_self.$myFunction.displayLog("需求列表读取失败："+JSON.stringify(data));
							uni.hideToast();
							uni.showToast({title: '连接失败...',icon: 'none'});
						}	
					}
				},
				fail: (data, code) => {
					uni.hideToast();
					uni.showToast({title: '连接失败...',icon: 'none'});
					// 刷新完毕后关闭刷新
					// uni.stopPullDownRefresh();
				}
			});
		},
		detail: function(e){
			uni.navigateTo({
				url:"../order/detail?id="+e.id
			})
		}		
	},
	components:{
		gracePage, graceFlex, graceNavBar
	}
}
</script>
<style>
	.grace-list-image{border-radius:10rpx;}
	.grace-list-title-text{font-size:medium !important;}
</style>